package com.hebeu.xwl.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.hebeu.xwl.pojo.Batch;
import com.hebeu.xwl.pojo.Result;
import com.hebeu.xwl.service.BatchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/batch")
public class BatchController {

    @Autowired
    private BatchService batchService;

    @GetMapping("/findAll")
    public Result<List<Batch>> findAll(){
        int userId = Integer.parseInt(((String)StpUtil.getLoginId()).split("%")[0]);
        Result result = new Result();
        try{
            List<Batch> batchList = batchService.findAll(userId);
            result.setCode(0);
            result.setMsg("success");
            result.setData(batchList);
        }catch (Exception e){
            result.setCode(101);
            result.setMsg(e.toString());
        }
        return result;
    }

    /**
     *
     * @param batch 需要以下参数：
     *              userId（必须有）
     *              ,description(对数据集的描述，建议设置默认值)
     *              ，name（数据集的名字，建议设置默认值）
     *              不清楚为空能不能添加
     * @return
     */
    @PostMapping("/add")
    public Result addBatch(@RequestBody Batch batch){
        int userId = Integer.parseInt(((String) StpUtil.getLoginId()).split("%")[0]);
        batch.setUserId(userId);
        Result result = batchService.addBatch(batch);
        return result;
    }

    @PostMapping("/delete")
    public Result deleteBatch(@RequestParam int[] ids){
        int userId = Integer.parseInt(((String)StpUtil.getLoginId()).split("%")[0]);
        Result result = new Result();
        try{
            batchService.deleteBatch(userId,ids);
            result.setCode(0);
            result.setMsg("success");
        }catch (Exception e){
            result.setCode(101);
            result.setMsg(e.toString());
        }
        return result;
    }

    /**
     *
     * @param batch 需要以下参数：
     *              batchId(必须有）
     *              userId（必须有）
     *              ,description(对数据集的描述，建议设置默认值)
     *              ，name（数据集的名字，建议设置默认值）
     *              不清楚为空能不能添加
     * @return
     */
    @PostMapping("/update")
    public Result updateBatch(@RequestBody Batch batch){
        Result result = new Result();
        try{
            batchService.updateBatch(batch);
            result.setCode(0);
            result.setMsg("success");
        }catch (Exception e){
            result.setCode(101);
            result.setMsg(e.toString());
        }
        return result;
    }

    @GetMapping("/findById")
    public Result<Batch> findById(@RequestParam int batchId){
        int userId = Integer.parseInt(((String)StpUtil.getLoginId()).split("%")[0]);
        Result result = new Result();
        try{
            Batch batch = batchService.findById(userId,batchId);
            result.setCode(0);
            result.setMsg("success");
            result.setData(batch);
        }catch (Exception e){
            result.setCode(101);
            result.setMsg(e.toString());
        }
        return result;
    }



    /**
     * 克隆数据集
     * @param batchId
     * @param description
     * @param name
     * @param batchId_old   被克隆的数据集id
     * @return
     */
    @PostMapping("/cloneBatch")
    public Result cloneBatch( @RequestParam int batchId,
                              @RequestParam String description,
                              @RequestParam String name,
                              @RequestParam int batchId_old){
        Batch batch1 = new Batch();
        batch1.setBatchId(batchId);
        batch1.setDescription(description);
        batch1.setName(name);

        int userId = Integer.parseInt(((String)StpUtil.getLoginId()).split("%")[0]);
        batch1.setUserId(userId);
        return batchService.cloneBatch(batch1,batchId_old);
    }

    /**
     *
     * @param batchId
     * @param description
     * @param name
     * @param batchId_01        被合并的数据集id1
     * @param batchId_02        被合并的数据集id2
     * @return
     */
    @PostMapping("/mergeBatch")
    public Result mergeBatch(@RequestParam int batchId,
                             @RequestParam String description,
                             @RequestParam String name,
                             @RequestParam int batchId_01,
                             @RequestParam int batchId_02){
        Batch batch1 = new Batch();
        batch1.setBatchId(batchId);
        batch1.setDescription(description);
        batch1.setName(name);

        int userId = Integer.parseInt(((String)StpUtil.getLoginId()).split("%")[0]);
        batch1.setUserId(userId);
        return batchService.mergeBatch(batch1,batchId_01,batchId_02);
    }
}

